package com.geekgo.lucenepp.service;

import com.geekgo.lucenepp.core.config.DataSourceIndex;
import com.geekgo.lucenepp.core.config.datasource.LuceneDataSource;
import com.geekgo.lucenepp.core.config.index.IndexFactory;
import com.geekgo.lucenepp.core.config.index.impl.AddIndex;
import com.geekgo.lucenepp.core.config.index.impl.DeleteIndex;
import com.geekgo.lucenepp.core.config.index.impl.QueryIndex;
import com.geekgo.lucenepp.core.config.index.impl.UpdateIndex;
import com.geekgo.lucenepp.model.Page;
import com.geekgo.lucenepp.util.DateUtil;
import com.geekgo.lucenepp.util.DocumentUtil;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;

/**
 * lucene服务实现
 *
 * @author HeHaoyang
 * @create 2017-06-14 10:19
 **/
public class LuceneServiceImpl implements LuceneService {

    private final Logger logger = LoggerFactory.getLogger(LuceneServiceImpl.class);

    private LuceneDataSource dataSource;

    public LuceneServiceImpl(LuceneDataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public boolean saveObj(Object obj) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(AddIndex :: new);
        boolean flag = create.saveIndex(dataSource, DocumentUtil.objtodoc(obj,dataSource.getXmlDem()));
        DateUtil.timeConsuming("添加1条索引",startTime);
        return flag;
    }

    @Override
    public boolean saveObjs(List<Object> objs) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(AddIndex :: new);
        boolean flag = create.saveIndexs(dataSource, DocumentUtil.objtodoc(objs,dataSource.getXmlDem()));
        DateUtil.timeConsuming("添加"+objs.size()+"索引",startTime);
        return flag;
    }

    @Override
    public boolean saveMaps(List<HashMap> objs) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(AddIndex :: new);
        boolean flag = create.saveIndexs(dataSource, DocumentUtil.maptodoc(objs,dataSource.getXmlDem()));
        DateUtil.timeConsuming("添加"+objs.size()+"索引",startTime);
        return flag;
    }

    @Override
    public boolean updateObj(Object obj, String key, String value) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(UpdateIndex:: new);
        boolean flag = create.updateIndex(dataSource, new Term(key,value),DocumentUtil.objtodoc(obj, dataSource.getXmlDem()));
        DateUtil.timeConsuming("更新索引",startTime);
        return flag;
    }

    @Override
    public boolean updateObjs(List<Object> objs, String key, String value) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(UpdateIndex:: new);
        boolean flag = create.updateIndexs(dataSource, new Term(key,value),DocumentUtil.objtodoc(objs, dataSource.getXmlDem()));
        DateUtil.timeConsuming("更新索引",startTime);
        return flag;
    }

    @Override
    public <T> List<T> findList(String value, Class<T> clazz) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(QueryIndex:: new);
        List<T> findList = create.findList(dataSource, value, clazz);
        DateUtil.timeConsuming("查询索引",startTime);
        return findList;
    }

    @Override
    public <T> Page<T> findPageList(String keyword, int pageNumber, int pageSize, Class<T> clazz) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(QueryIndex:: new);
        Page<T> page = create.findPageList(dataSource, keyword,pageNumber,pageSize,clazz);
        DateUtil.timeConsuming("查询索引",startTime);
        return page;
    }

    @Override
    public <T> List<T> findRangeList(Query query, Class<T> clazz) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(QueryIndex:: new);
        List<T> findList = create.findRangeList(dataSource,query, clazz);
        DateUtil.timeConsuming("查询索引",startTime);
        return findList;
    }

    @Override
    public <T> Page<T> findRangePageList(Query query, int pageNumber, int pageSize, Class<T> clazz) {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(QueryIndex:: new);
        Page<T> page = create.findRangePageList(dataSource,query,pageNumber,pageSize,clazz);
        DateUtil.timeConsuming("查询索引",startTime);
        return page;
    }

    @Override
    public boolean delAll() {
        long startTime = DateUtil.getDate();
        IndexFactory create = DataSourceIndex.create(DeleteIndex:: new);
        boolean flag = create.delAll(dataSource);
        DateUtil.timeConsuming("删除索引", startTime);
        return flag;
    }
}
